package com.quick.controller.monitor;


import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.common.utils.poi.ExcelUtil;
import com.quick.system.domain.SysOperateLog;
import com.quick.system.service.SysOperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 操作日志
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@RestController
@RequestMapping("/monitor/operateLog")
public class SysOperateLogController extends BaseController {
    @Autowired
    private SysOperateLogService sysOperateLogService;

    @PreAuthorize("@ps.hasPermission('monitor:operateLog:list')")
    @GetMapping("/list")
    public ApiResponse list(SysOperateLog operateLog) {
        try {
            startPage();
            List<SysOperateLog> list = sysOperateLogService.selectSysOperateLogList(operateLog);
            return paging(list);
        } finally {
            clean();
        }
    }

    @Log(title = "操作日志", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ps.hasPermission('monitor:operateLog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperateLog operateLog) {
        List<SysOperateLog> list = sysOperateLogService.selectSysOperateLogList(operateLog);
        ExcelUtil<SysOperateLog> util = new ExcelUtil<>(SysOperateLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@ps.hasPermission('monitor:operateLog:remove')")
    @DeleteMapping("/{operateIds}")
    public ApiResponse remove(@PathVariable Long[] operateIds) {
        return success(sysOperateLogService.deleteSysOperateLogByOperateIds(operateIds));
    }

    @Log(title = "操作日志", businessType = BusinessTypeEnum.CLEAN)
    @PreAuthorize("@ps.hasPermission('monitor:operateLog:remove')")
    @DeleteMapping("/clean")
    public ApiResponse clean() {
        sysOperateLogService.cleanOperateLog();
        return success();
    }
}
